package com.android.danaobank.api;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.android.danaobank.model.HttpResponse;
import com.android.danaobank.model.response.Advertisement;
import com.android.danaobank.model.response.MallHeadline;
import com.android.danaobank.model.response.MiddleClass;
import com.android.danaobank.model.response.ProductResponse;
import com.android.danaobank.model.response.ShopClassChildBean;
import com.android.pdd.baseapp.api.ApiManager;
import com.blankj.utilcode.util.EncryptUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import io.reactivex.Observable;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.http.Body;

public class ApiClient extends ApiManager<ApiService> {
    private static ApiClient instance;

    private ApiClient() {

    }

    public static ApiClient getInstance() {
        if (instance == null) {
            synchronized (ApiClient.class) {
                if (instance == null) {
                    instance = new ApiClient();
                }
            }
        }
        return instance;
    }

    @Override
    protected OkHttpClient initOkHttp() {

        return super.initOkHttp();
    }

    @Override
    protected String baseUrl() {
        return "https://m.yoao.com/";
    }

    public static String sign(Map<String, Object> map) {
        String str = "qO7sSGbVVmPxaL3CbTYWxg==";
        map.put("version", "v1.0");
        map.put("sysType", "android");
        map.put("uniqueId", UUID.randomUUID().toString());

        List<String> arrayList = new ArrayList();
        arrayList.addAll(map.keySet());
        Collections.sort(arrayList);
        StringBuffer stringBuffer = new StringBuffer();
        for (String str2 : arrayList) {
            stringBuffer.append(str2);
            stringBuffer.append("=");
            stringBuffer.append(map.get(str2));
            stringBuffer.append("&");
        }
        stringBuffer.append(str);
        return EncryptUtils.encryptMD5ToString(stringBuffer.toString()).toLowerCase();
    }

    public Observable<HttpResponse<List<MiddleClass>>> getMiddleClass() {
        Map<String, Object> map = new HashMap<>();
        map.put("subCode", "1597217409418011323567");
        String sign = sign(map);
        map.put("sign", sign);
        map.put("timestamp", System.currentTimeMillis());
        return request(apiService.getMiddleClass(buildBody(map)));
    }

    public Observable<HttpResponse<List<Advertisement>>> getAdvertisementByType(String type) {
        Map<String, Object> map = new HashMap<>();
//        map.put("type", "mallIndexPosition");
        map.put("type", type);
        String sign = sign(map);
        map.put("sign", sign);
        map.put("timestamp", System.currentTimeMillis());
        return request(apiService.getAdvertisementByType(buildBody(map)));
    }

    public Observable<HttpResponse<List<MallHeadline>>> mallHeadlines() {
        Map<String, Object> map = new HashMap<>();
        map.put("pageCount", "21");
        map.put("subCode", "1597217409418011323567");
        map.put("sortType", "");
        map.put("pageSize", "1");
        map.put("showWhere", "1");
        String sign = sign(map);
        map.put("sign", sign);
        map.put("timestamp", System.currentTimeMillis());
        return request(apiService.mallHeadlines(buildBody(map)));
    }

    /**
     * 商城-推荐 商品
     **/
    public Observable<HttpResponse<ProductResponse>> getCommentProductList(int pageSize, int pageCount, JSONObject jsonObject) {
        Set<String> keySet = jsonObject.keySet();
        Map<String, Object> map = new HashMap<>();
        for (String key : keySet) {
            map.put(key, jsonObject.getString(key));
        }
        map.put("pageCount", pageCount);
//        map.put("api_url", "\\/api\\/shop\\/getCommentProductList.json");
        map.put("subCode", "1597217409418011323567");
//        map.put("orderBy", "1");
        map.put("pageSize", pageSize);
        String sign = sign(map);
        map.put("sign", sign);
        map.put("timestamp", System.currentTimeMillis());
        return request(apiService.getCommentProductList(buildBody(map)));
    }

    /**
     * 商城-礼品 分类
     **/
    public Observable<HttpResponse<List<ShopClassChildBean>>> getNextLevel(@Body RequestBody body) {
        Map<String, Object> map = new HashMap<>();
        map.put("levelCode", "1562953596935434437938");
        String sign = sign(map);
        map.put("sign", sign);
        map.put("timestamp", System.currentTimeMillis());
        return request(apiService.getNextLevel(buildBody(map)));
    }


}
